﻿using CheccCoverplateCulvertAssemble.Common;
using CheccCoverplateCulvertAssemble.Common.Http;
using CheccCoverplateCulvertAssemble.Entity;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CheccCoverplateCulvertAssemble.FormColl
{
    public partial class SeparatingRebarForm : Form
    {
        private string _generalDiagram;
        private string _clearSpanAndHeight;
        private double _depthOfFill;
        public SeparatingRebarForm(string generalDiagram, string clearSpanAndHeight, double depthOfFill)
        {
            #region 界面设置
            //使最大化窗口失效
            this.MaximizeBox = false;
            //禁止对窗口大小进行拖拽
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            //窗口位置
            this.StartPosition = FormStartPosition.CenterScreen;
            //窗口置顶
            this.TopMost = true;
            #endregion
            InitializeComponent();
            //窗体控件适应屏幕分辨率
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;
            this._generalDiagram = generalDiagram;
            this._clearSpanAndHeight = clearSpanAndHeight;
            this._depthOfFill = depthOfFill;
            this.Icon = Properties.Resources.Culvert;
        }

        private void SeparatingRebarForm_Load(object sender, EventArgs e)
        {
            dgv_Rebar1.ClearSelection();
            dgv_Rebar2.ClearSelection();

            dgv_Rebar1.Enabled = false;
            dgv_Rebar2.Enabled = false;

            this.txt_BracingBeamHeight.ReadOnly = true;
            this.txt_BracingBeamInterval.ReadOnly = true;
            this.txt_BracingBeamLength.ReadOnly = true; 
            this.txt_BracingBeamWidth.ReadOnly = true;  

            SetDataGridViewData(_generalDiagram, _clearSpanAndHeight, _depthOfFill);
        }
        private void SetDataGridViewData(string generalDiagram, string clearSpanAndHeight,double depthOfFill)
        {
            HttpRequestData httpRequestData = HttpRequestData.CreateHttpRequestData();
            //尺寸
            List<SupportbeamDimension> supportbeamDimensions = httpRequestData.SelectAssembleSupportBeamDimension(generalDiagram);
            for (int i = 0; i < supportbeamDimensions.Count; i++)
            {
                if (supportbeamDimensions[i].standardDrawing== generalDiagram && supportbeamDimensions[i].clearSpan == clearSpanAndHeight)
                {
                    SpiltTh(supportbeamDimensions[i].fillHeight, out double min, out double max);
                    if (depthOfFill>= min && depthOfFill <= max)
                    {
                        this.txt_BracingBeamLength.Text = supportbeamDimensions[i].length.ToString();
                        this.txt_BracingBeamHeight.Text = supportbeamDimensions[i].height.ToString();
                        this.txt_BracingBeamWidth.Text = supportbeamDimensions[i].width.ToString();
                        break;
                    }
                }
            }
            //支撑梁1号钢筋
            List<SupportbeamNo1Rebar> supportbeamNo1Rebars = httpRequestData.SelectAssembleBracebeamNo1s(generalDiagram);
            foreach (var item in supportbeamNo1Rebars)
            {
                if (item.standardDrawing == generalDiagram && item.clearSpan == clearSpanAndHeight)
                {
                    SpiltTh(item.fillHeight, out double min, out double max);
                    if (depthOfFill >= min && depthOfFill <= max)
                    {
                        dgv_Rebar1.Rows.Add(item.reinforcementType,item.reinforcementDiameter, item.numberBars,item.hookLength);
                        break;
                    }
                }
            }

            //支撑梁2号钢筋
            List<SupportbeamNo2Rebar> supportbeamNo2Rebars = httpRequestData.SelectAssembleBracebeamNo2s(generalDiagram);
            foreach (var item in supportbeamNo2Rebars)
            {
                if (item.standardDrawing == generalDiagram && item.clearSpan == clearSpanAndHeight)
                {
                    SpiltTh(item.fillHeight, out double min, out double max);
                    if (depthOfFill >= min && depthOfFill <= max)
                    {
                        dgv_Rebar2.Rows.Add(item.reinforcementType, item.reinforcementDiameter, item.reinforcementAssembly, item.hookLength);
                        break;
                    }
                }
            }

        }
        private void SpiltTh(string depthOfFillTh, out double start, out double end)
        {
            //拆解填土高度
            string newDepthOfFillTh = "";
            for (int i = 1; i < depthOfFillTh.Length - 1; i++)
            {
                newDepthOfFillTh += depthOfFillTh[i];
            }
            string[] ttgd = newDepthOfFillTh.Split(',');
            if (ttgd.Length < 2)
                ttgd = newDepthOfFillTh.Split('，');
            if (depthOfFillTh[0].ToString() == "(")
                start = Convert.ToDouble(ttgd[0]) + 0.0001;
            else
                start = Convert.ToDouble(ttgd[0]);
            if (depthOfFillTh[depthOfFillTh.Length - 1].ToString() == ")")
                end = Convert.ToDouble(ttgd[1]) - 0.0001;
            else
                end = Convert.ToDouble(ttgd[1]);
        }

        private void UnitaryRebarForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            //RebarEntity rebarData = RebarEntity.RebarEntityObj();
            //rebarData.dgv_rebar1 = convertDgv(this.dgv_Rebar1);
            //rebarData.dgv_Rebar2 = convertDgv(this.dgv_Rebar2);
        }

        private DataGridView convertDgv(DataGridView dgv)
        {
            DataGridView dataGridView = new DataGridView();
            dataGridView.RowHeadersVisible = false;
            dataGridView.AllowUserToAddRows = false;
            foreach (DataGridViewColumn c in dgv.Columns)
            {
                dataGridView.Columns.Add(c.Clone() as DataGridViewColumn);
            }
            foreach (DataGridViewRow r in dgv.Rows)
            {
                int index = dataGridView.Rows.Add(r.Clone() as DataGridViewRow);
                foreach (DataGridViewCell o in r.Cells)
                {
                    dataGridView.Rows[index].Cells[o.ColumnIndex].Value = o.Value;
                }
            }
            return dataGridView;
        }
    }
}
